#  Copyright (C) 2010 Luca Piccinelli
# 
#  This program is free software: you can redistribute it and/or modify
#  it under the terms of the GNU General Public License as published by
#  the Free Software Foundation, either version 3 of the License, or
#  (at your option) any later version.
#  
#  This program is distributed in the hope that it will be useful,
#  but WITHOUT ANY WARRANTY; without even the implied warranty of
#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#  GNU General Public License for more details.
#  
#  You should have received a copy of the GNU General Public License
#  along with this program.  If not, see <http://www.gnu.org/licenses/>.

'''
Created on Jun 16, 2011

@author: picci
'''
from PyQt4 import QtGui, QtCore
from pipeit import PDLElementVisitor

class QtGuiPDLVisitor(PDLElementVisitor):
    def __init__(self, qt_element = None):
        super().__init__()
        self.qt_element = qt_element
    
    def visitPDLLiteral(self, literal):
        self.qt_element.addItem(str(literal.get_value()), literal.get_value());
        
    def visitPDLPair(self, pair):
        self.qt_element.addItem( str(pair.get_label()), pair.get_value() );
        
    def visitPDLRange(self, range):
        qSlider = QtGui.QSlider(self.qt_element.parentWidget())
        qSlider.setProperty("scale", 1.0 / range.get_scale())
        qSlider.setMinimumWidth(100)
        qSlider.setRange(range.get_from(), range.get_to())    
        qSlider.setSingleStep(range.get_step())
        qSlider.setOrientation(QtCore.Qt.Horizontal)
    
        self.qt_element.addWidget(qSlider)
    
        return range.get_scale()
    
    def visitPDLComposite(self, composite):
        combo = QtGui.QComboBox(self.qt_element.parentWidget())
        composite_iterator = composite.begin()
        while composite_iterator.has_next(): 
            el = composite_iterator.nextSerializable()
            visitor = QtGuiPDLVisitor(combo)
            el.serialize(visitor)
        
        self.qt_element.addWidget(combo)